/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.risesoft.risebi.collection.dao.ReportWindowPeriodDao;
import net.risesoft.risebi.collection.dao.TemplateDao;
import net.risesoft.risebi.collection.model.ReportPeriod;
import net.risesoft.risebi.collection.model.ReportWindowPeriod;
import net.risesoft.risebi.collection.model.Template;
import net.risesoft.risebi.collection.service.ReportWindowPeriodManager;
import net.risesoft.risebi.commons.util.GUID;

import org.appfuse.dao.Dao;
import org.appfuse.service.impl.BaseManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author <a href="mailto: baiyibing@gmail.com">王锤</a>
 * @version $Revision$
 * 
 */
@Repository(value="reportWindowPeriodManager")
@Transactional(rollbackFor={java.lang.Exception.class})
public class ReportWindowPeriodManagerImpl extends BaseManager implements ReportWindowPeriodManager {
	
	private ReportWindowPeriodDao reportWindowPeriodDao;

	public ReportWindowPeriodDao getReportWindowPeriodDao() {
		return reportWindowPeriodDao;
	}

	@Autowired
	public void setReportWindowPeriodDao(ReportWindowPeriodDao reportWindowPeriodDao) {
		this.reportWindowPeriodDao = reportWindowPeriodDao;
	}
	
	private TemplateDao templateDao;
	

	public TemplateDao getTemplateDao() {
		return templateDao;
	}

	@Autowired
	public void setTemplateDao(TemplateDao templateDao) {
		this.templateDao = templateDao;
	}
	
	public List<ReportWindowPeriod> getWindowReportPeriodsByTid(
			String templateid) throws Exception {
		return getReportWindowPeriodDao().getWindowReportPeriodsByTid(templateid);
	}

	public Map getPagedByAppTemplateGuid(String appGuid, String templateGuid, int start, int limit) throws Exception {
		String hql = "from ReportWindowPeriod t where t.template.guid='" + templateGuid + "' and t.template.application.guid='" + appGuid + "'";
		String countHql = "select count(*) " + hql;
		Integer count = this.getCount(countHql);
		
		String orderByHql = hql + " order by t.reportPeriod.order_in_year asc";
		
		List list = this.find(orderByHql, start, limit);
		Map map = new HashMap();
		map.put("count", count);
		map.put("list", list);
		
		return map;
	}
	
	public List<ReportWindowPeriod> getWindowReportPeriodsByPeriodId(
			String periodId) throws Exception {
		return getReportWindowPeriodDao().getWindowReportPeriodsByPeriodId(periodId);
	}

	public void initReportWindowPeriod(String templateGuid) throws Exception {
		List<ReportWindowPeriod> rwps = this.getWindowReportPeriodsByTid(templateGuid);
		
		Map<String,ReportWindowPeriod> map = new HashMap<String, ReportWindowPeriod>();
		for(ReportWindowPeriod rwp:rwps){
			ReportPeriod rp = rwp.getReportPeriod();
			map.put(rp.getGuid(), rwp);
		}
		
//		reportWindowPeriodDao.removeWindowReportPeriodsByTid(templateGuid);
		Template template = (Template)templateDao.getObject(Template.class, templateGuid);		
		List<ReportPeriod> reportPeriods = template.getReportPeriodType().getReportPeriods();
		reportPeriods = (List<ReportPeriod>)this.filter(reportPeriods, "where this.service.guid='" + template.getApplication().getServiceGuid() + "'");
	
		List<ReportWindowPeriod> reportWindowPeriods = new ArrayList<ReportWindowPeriod>();
		for(int i=0;i<reportPeriods.size();i++){
			ReportPeriod reportPeriod = reportPeriods.get(i);
			
			Object o = map.get(reportPeriod.getGuid());
			if(o==null){
				ReportWindowPeriod reportWindowPeriod = new ReportWindowPeriod();
				reportWindowPeriod.setGuid(new GUID().toString());
				reportWindowPeriod.setReportPeriod(reportPeriod);
				reportWindowPeriod.setTemplate(template);			
				reportWindowPeriod.setLateDate(reportPeriod.getEnd_time());
				reportWindowPeriod.setLockDate(reportPeriod.getEnd_time());
				reportWindowPeriod.setUnlockDate(reportPeriod.getBegin_time());
				reportWindowPeriod.setUrgeDate(reportPeriod.getTimetype());
				reportWindowPeriods.add(reportWindowPeriod);
			}
		}		
		reportWindowPeriodDao.saveObjects(reportWindowPeriods);	
	}
	
	public List<ReportWindowPeriod> getWindowReportPeriodsByDate(Date date) throws Exception{
		return getReportWindowPeriodDao().getWindowReportPeriodsByDate(date);	
	}
	
	/* (non-Javadoc)
	 * @see org.appfuse.service.Manager#getDao()
	 */
	public Dao getDao() {
		return reportWindowPeriodDao;
	}

}
